﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTurnClient.Server
{
    public class TimeService
    {
        public TimeService()
        {
        }
        public bool IsTime(List<JobTime> jobTimeList, List<string> NewTime)
        {
            bool isCan = true;
            for (int i = 0; i < 3; i++)
            {
                if (jobTimeList[i].IsStart == 1)
                {
                    if (NewTime[i] != jobTimeList[i].JobRunTime)
                    {
                        isCan = false;
                    }
                }
                if (jobTimeList[i].IsStart == 2)
                {
                    if ((Convert.ToDecimal(NewTime[i]) / Convert.ToDecimal(jobTimeList[i].JobRunTime)).ToString().Contains('.'))
                    {
                        isCan = false;
                    }
                    else
                    {
                        if (i == 0)
                        {
                            if ((NewTime[1] != jobTimeList[1].JobRunTime))
                            {
                                isCan = false;
                            }
                            if ((NewTime[2] != jobTimeList[2].JobRunTime))
                            {
                                isCan = false;
                            }
                        }
                        if (i == 1)
                        {
                            if ((NewTime[2] != jobTimeList[2].JobRunTime))
                            {
                                isCan = false;
                            }
                        }
                    }
                }
                if (jobTimeList[i].IsStart == 3)
                {
                    if (DateTime.Now.Hour < Convert.ToInt32(jobTimeList[i].JobRunTime.Substring(0, 1)) || DateTime.Now.Hour > Convert.ToInt32(Math.Abs(Convert.ToInt32(jobTimeList[i].JobRunTime.Substring(1, 1)) + 12)))
                    {
                        isCan = false;
                        //Console.WriteLine(DateTime.Now.Hour);
                    }
                    else
                    {
                        var TimeTypethree = jobTimeList[3];
                        if (TimeTypethree.IsStart == 1)
                        {
                            if (NewTime[i] != TimeTypethree.JobRunTime)
                            {
                                isCan = false;
                            }
                        }
                        if (TimeTypethree.IsStart == 2)
                        {
                            if ((Convert.ToDecimal(NewTime[i]) / Convert.ToDecimal(TimeTypethree.JobRunTime)).ToString().Contains('.'))
                            {
                                isCan = false;
                            }
                            else
                            {
                                if (i == 0)//如果是时的单位被定义
                                {
                                    if ((NewTime[1] != jobTimeList[1].JobRunTime))
                                    {
                                        isCan = false;
                                    }
                                    if ((NewTime[2] != jobTimeList[2].JobRunTime))
                                    {
                                        isCan = false;
                                    }
                                }
                                if (i == 1)//如果是分的单位被定义
                                {
                                    if ((NewTime[2] != jobTimeList[2].JobRunTime))
                                    {
                                        isCan = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return isCan;
        }

        public  bool IsWeek(string jobWeek)
        {
            List<string> week = jobWeek.Split(',').ToList();
            return week.Exists(x => x == JsonConvert.SerializeObject(DateTime.Now.DayOfWeek));
        }

    }
}
